<?php

class ImageUploader{

	private $max_size;
	private $upload_dir;
	
	private $file_name;
	private $file_size;
	private $file_tmp;
	private $file_type;
	
	private $image_name; 
	
	
	function __construct( $max_size, $upload_dir)
	{
		$this->max_size = $max_size;
		$this->upload_dir = $upload_dir;
	}
	

	function setImage($name)
	{
		$this->file_name = $_FILES[$name]['name'];
		$this->file_size = $_FILES[$name]['size'];
		$this->file_tmp = $_FILES[$name]['tmp_name'];
		$this->file_type = $_FILES[$name]['type'];
	}
	

	 
	function setImageName($name)
	{
		$this->image_name = $_FILES[$name]['name'];
	}
	
	
	
	function deleteExisting()
	{
		$jpg =  $this->upload_dir.''.$this->image_name.'.jpg';
		if(file_exists($jpg)) unlink($jpg);
				
		$jpeg =  $this->upload_dir.''.$this->image_name.'.jpeg';
		if(file_exists($jpeg)) unlink($jpeg);		
			
		$gif =  $this->upload_dir.''.$this->image_name.'.gif';
		if(file_exists($gif)) unlink($gif); 		
			
		$png =  $this->upload_dir.''.$this->image_name.'.png';
		if(file_exists($png)) unlink($png);		
	}
	
	
	function upload()
	{
		$ext = strrchr($this->file_name, '.');
		$name = $this->upload_dir.''.$this->image_name.''.$ext;
		if(!move_uploaded_file($this->file_tmp, $name)){
			echo $_FILES[$this->file_name]['error'];
			return false;
		}
		else
			return true;
	}
	
	
	function checkSize()
	{
		if($this->file_size > ($this->max_size))
			return false;
		else
			return true;
	}
	

	function checkExt()
	{		
		if (($this->file_type != 'image/jpg') && ($this->file_type != 'image/jpeg') && 
				($this->file_type != 'image/gif') && ($this->file_type != 'image/png'))
			return false;
		else
			return true;		
	}
}
?>